/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:何人亮
 * Date:2024-08-25
 * 15:45
 */
public class Test {
    public static void main(String[] args) {

    }
}
class Solution {
    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
    public int preindex;
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildTreeChild(preorder,inorder,0,preorder.length-1);
    }

    private TreeNode buildTreeChild(int[] preorder, int[] inorder, int begin, int inend) {
        if(begin>inend){
            return null;
        }
        TreeNode root=new TreeNode(preorder[preindex]);
        int rootIndex=find(inorder,inend,preorder[preindex]);
        preindex++;
        root.left=buildTreeChild(preorder,inorder,begin,rootIndex-1);
        root.right=buildTreeChild(preorder,inorder,rootIndex+1,inend);
        return root;
    }
    public int find( int[] inorder, int inend,int val){
        for(int j=0;j<=inend;j++){
            if(val==inorder[j]){
                return j;
            }
        }
        return -1;
    }
}